package com.meitianhui.convenitenbuy.module.mine.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.mine.adapter.GuiderHistoryRequestAdapter;
import com.meitianhui.convenitenbuy.module.mine.contract.GuiderHistoryRequestContract;
import com.meitianhui.convenitenbuy.module.mine.di.DaggerMineComponent;
import com.meitianhui.convenitenbuy.module.mine.di.MineModule;
import com.meitianhui.convenitenbuy.module.mine.model.ShoppersHistoricalBean;
import com.meitianhui.convenitenbuy.module.mine.presenter.GuiderHistoryRequestPresenter;
import com.meitianhui.convenitenbuy.util.TipUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/*
 * Created by amy on 2017/11/27.
 */

public class GuiderHistoryRequestActivity extends BaseToolbarActivity implements GuiderHistoryRequestContract.View, BaseQuickAdapter.RequestLoadMoreListener {
    private RecyclerView mRlvGuiderHistoryRequest;
    private LinearLayout mLlGuiderHistoryRequestNone;
    private int pageIndex = 1;
    private int pageSize = 10;
    @Inject
    GuiderHistoryRequestPresenter mGuiderHistoryRequestPresenter;

    List<ShoppersHistoricalBean.Convient> mGuiderHistoryList = new ArrayList<>();
    private GuiderHistoryRequestAdapter mAdapter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerMineComponent.builder().mineModule(new MineModule(this)).appComponent(App.getInstance().getAppComponent()).build().inject(this);
        setTbTitle("历史记录");
        initId();
        initData();
    }

    private void initData() {
        if (mAdapter == null) {
            mAdapter = new GuiderHistoryRequestAdapter(mGuiderHistoryList);
        }
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRlvGuiderHistoryRequest.setLayoutManager(manager);
        mRlvGuiderHistoryRequest.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(this, mRlvGuiderHistoryRequest);

        getGuiderHistoryList();
    }

    private void getGuiderHistoryList() {
        mGuiderHistoryRequestPresenter.getGuiderHistory(pageIndex, pageSize);
    }

    private void initId() {
        mRlvGuiderHistoryRequest = getViewById(R.id.rlv_guider_history_request);
        mLlGuiderHistoryRequestNone = getViewById(R.id.ll_guider_history_request_none);
    }

    @Override
    protected int setView() {
        return R.layout.activity_guider_history_request;
    }

    @Override
    protected void onRefreshData() {
        if (mAdapter.isLoading())
            mAdapter.loadMoreEnd();
        pageIndex = 1;
        getGuiderHistoryList();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAdapter.isLoading() && mAdapter != null)
            mAdapter.loadMoreEnd();
        refreshFinish();
    }

    @Override
    public void showError(String msg) {
        mRlvGuiderHistoryRequest.setVisibility(View.GONE);
        mLlGuiderHistoryRequestNone.setVisibility(View.VISIBLE);
        TipUtils.showToast(App.getInstance(), msg, 3);
    }

    @Override
    public void showEmpty() {
        mRlvGuiderHistoryRequest.setVisibility(View.GONE);
        mLlGuiderHistoryRequestNone.setVisibility(View.VISIBLE);
        TipUtils.showToast(App.getInstance(), "暂无历史记录", 3);
    }

    @Override
    public void getGuiderHistorySucc(List<ShoppersHistoricalBean.Convient> guiderHistoryList) {

        mRlvGuiderHistoryRequest.setVisibility(View.VISIBLE);
        mLlGuiderHistoryRequestNone.setVisibility(View.GONE);

        if (mAdapter.isLoading())
            mAdapter.loadMoreComplete();
        if (pageIndex == 1) {
            mAdapter.replaceData(guiderHistoryList);
        } else
            mAdapter.addData(guiderHistoryList);

        mAdapter.setEnableLoadMore(guiderHistoryList.size() < pageSize ? false : true);
        refreshFinish();
    }

    @Override
    public void onLoadMoreRequested() {
        pageIndex++;
        getGuiderHistoryList();
    }
}
